package com.haiyou.dc.mq.service.impl;

import com.haiyou.common.base.AbstractBaseService;
import com.haiyou.common.exception.BusinessException;
import com.haiyou.dc.mq.entity.Product;
import com.haiyou.dc.mq.entity.ProductRobbingRecord;
import com.haiyou.dc.mq.mapper.ProductMapper;
import com.haiyou.dc.mq.service.ProductRobbingRecordService;
import com.haiyou.dc.mq.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author root
 * @date 2020-01-11 22:23:44
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ProductServiceImpl extends AbstractBaseService<ProductMapper, Product> implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Autowired
    private ProductRobbingRecordService productRobbingRecordService;

    @Override
    public void updateProduct(String mobile) {
        log.info("----------------->当前手机号{}----------------->", mobile);
        try {

            Product product = productMapper.selectProduct(1);
            log.info("------------->product={}", product);
            if (product != null && product.getTotal() > 0) {
                Integer count = productMapper.updateProduct(product.getProductNo());
                log.info("------------->count={}", count);
                if (count > 0) {
                    ProductRobbingRecord productRobbingRecord = new ProductRobbingRecord();
                    productRobbingRecord.setProductId(product.getId());
                    productRobbingRecord.setRobbingTime(new Date());
                    productRobbingRecord.setMobile(String.valueOf(mobile));
                    productRobbingRecordService.insert(productRobbingRecord);
                }
            }
        } catch (BusinessException ex) {
            log.error("处理抢单发生异常：mobile={} ", mobile);
        }
    }
}